//
//  ZJActivityVC.m
//  这家测试项目
//
//  Created by IOS_DEVELOPER on 2017/7/7.
//  Copyright © 2017年 陈志凌. All rights reserved.
//

#import "ZJActivityVC.h"
#import "NJKWebViewProgressView.h"
#import "NJKWebViewProgress.h"
#import "ZJExtensionAgentController.h"
@interface ZJActivityVC ()<UIWebViewDelegate,NJKWebViewProgressDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webview;
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;

//进度条
@property (strong, nonatomic)NJKWebViewProgressView *progressView;
@property (strong, nonatomic)NJKWebViewProgress *progressProxy;
@end

@implementation ZJActivityVC
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    _progressView.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.sureBtn.hidden = NO;
    [self.sureBtn setBackgroundColor:[Utils colorWithHexString:allConstHexColor]];
    _webview.scrollView.bounces = NO;
    _webview.scrollView.showsHorizontalScrollIndicator = NO;
    _webview.scrollView.showsVerticalScrollIndicator = NO;
    _webview.opaque = NO;
    
    //添加进度条
    _progressProxy= [[NJKWebViewProgress alloc] init];
    _webview.delegate = _progressProxy;
    _progressProxy.webViewProxyDelegate = self;
    _progressProxy.progressDelegate = self;
    
    CGFloat progressBarHeight = 2.f;
    CGRect navigationBarBounds = self.navigationController.navigationBar.bounds;
    CGRect barFrame = CGRectMake(0, navigationBarBounds.size.height - progressBarHeight, navigationBarBounds.size.width, progressBarHeight);
    _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
    _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    
    [self.navigationController.navigationBar addSubview:_progressView];
    
    NSURL *url = [NSURL URLWithString:_model.link_url?:self.urlStr];
    
    if (self.urlStr){
        
        self.sureBtn.hidden = YES;
    }else if (self.model && [self.model.enroll integerValue] !=1){
        
        self.sureBtn.hidden = YES;
    }
    
    if ([USER_DEFAULTS objectForKey:@"activity_id"]) {
        NSMutableArray *mutableCopyArr = [[USER_DEFAULTS mutableArrayValueForKey:@"activity_id"] mutableCopy];
        if ([mutableCopyArr containsObject:_model.activity_id]) {
            self.sureBtn.hidden = YES;
        }
    }
    
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_webview loadRequest:request];
}

- (IBAction)sureBtn:(id)sender {
    
    if([self.model.enroll integerValue] == 1){
    
    NSDictionary *parameter = [LRNetwork requestParameter:@{@"activity_id":_model.activity_id}];
    NSString *url = [LRNetwork requestURL:@"cgi/activity_enroll.php"];
    
    [HUD showMsg:@"报名中..."];
    
    [LRNetwork netRequestPOSTWithRequestURL:url withParameter:parameter withReturnValeuBlock:^(id returnValue) {
        
        [HUD showSucceedMsg:@"报名成功!"];
        self.sureBtn.hidden = YES;
        
        NSMutableArray *projArr = [NSMutableArray array];
        [projArr addObject:_model.activity_id];
        [USER_DEFAULTS setObject:projArr forKey:@"activity_id"];
        [USER_DEFAULTS synchronize];
        
    } withErrorCodeBlock:^(NSInteger errorCode, NSString *errorMessage) {
        [HUD showErrorMsg:errorMessage];
    } withFailureBlock:^{
        
    }];
        
    }
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{

    _progressView.hidden = YES;
}

#pragma mark - NJKWebViewProgressDelegate
-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
    [_progressView setProgress:progress animated:YES];
}
@end
